import Vue from 'vue'
import VueRouter from 'vue-router'
import False from '../components/404.vue'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Show from '../components/edit/Show.vue'
import School from '../components/edit/school.vue'
import Computer from '../components/edit/computer.vue'
import Economy from '../components/edit/economy.vue'
import Electron from '../components/edit/electron.vue'
import Automate from '../components/edit/automate.vue'
import Machine from '../components/edit/machine.vue'
import Welcome from '../components/Welcome.vue'
import Add from '../components/edit/Add.vue'
import AddShow from '../components/edit/addShow.vue'
// import Editor from '../components/edit/QuillEditor.vue'
import Editor from '../components/Tinymce/components/editorImage.vue'
import Branch from '../components/branch/branchEdit.vue'
import addBranch from '../components/branch/addBranch.vue'
// import { reject } from 'core-js/fn/promise'
// import Syj from '../components/syj.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },  // redirect重定向,访问跟路径是自动跳转到/login组件
  { path: '/login', component: Login }, // 路由规则
  { path: '/home',
    component: Home, 
    redirect: '/show',
    children: [
      // {path: '/welcome', component: Welcome},
      {path: '/show', component: Show},
      {path: '/school', component: School},
      {path: '/computer', component: Computer},
      {path: '/economy', component: Economy},
      {path: '/electron', component: Electron},
      {path: '/automate', component: Automate},
      {path: '/machine', component: Machine},
      {
        path: '/show/add',
        component: Add,
        children: [
          // {path: '/show/add/quillEditor', component: Editor}
          {path: '/show/add/quillEditor', component: Editor}
        ]
      },
      {path: '/show/addshow', component: AddShow},
      {path: '/branch/edit', component: Branch},
      {path: '/branch/addBranch', component: addBranch},
      // {path: '/syj', component: Syj},
    ]
  },
  {
      // 匹配所有路径  vue2使用*   vue3使用/:pathMatch(.*)*或/:pathMatch(.*)或/:catchAll(.*)
      path: "*",
      meta: {
          title: '页面不存在'
      },
      name: "404",
      component: False
          
  }
]


const router = new VueRouter({
  routes
})

// 挂载全局前置路由导航守卫
router.beforeEach((to, from, next) => {
  // to即将要进入的地址
  if(to.path === '/login') return next()

  // // 获取token ====>  如果登录有进行token判断
  let tokenStr = window.localStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()

})

export default router
